package com.read.service.impl;

import com.github.pagehelper.PageHelper;
import com.read.dto.params.NovelNameParamsDto;
import com.read.dto.result.NovelContentResultDto;
import com.read.dto.result.NovelNameResultDto;
import com.read.dto.result.NovelSectionResultDto;
import com.read.dto.result.NovelTypeResultDto;
import com.read.mapper.NovelContentMapper;
import com.read.mapper.NovelNameMapper;
import com.read.mapper.NovelSectionMapper;
import com.read.mapper.NovelTypeMapper;
import com.read.service.NovelService;
import com.read.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author sh-sunb6
 * @description
 * @date 2019-02-19 17:23
 */
@Service
public class NovelServiceImpl implements NovelService {

    @Autowired
    private NovelNameMapper novelNameMapper;

    @Autowired
    private NovelSectionMapper novelSectionMapper;

    @Autowired
    private NovelContentMapper novelContentMapper;

    @Autowired
    private NovelTypeMapper novelTypeMapper;

    @Override
    public List<NovelNameResultDto> selectNovelByParams(NovelNameParamsDto paramsDto) {
        PageHelper.startPage(paramsDto.getPageNum(), paramsDto.getPageSize());
        return novelNameMapper.selectNovelByParams(paramsDto);
    }



    @Override
    public List<NovelSectionResultDto> selectNovelSectionByNovelId(int novelId, int pageNum, int pageSize) {

        PageHelper.startPage(pageNum, pageSize);
        return novelSectionMapper.selectNovelSectionByNovelId(novelId);
    }

    @Override
    public Map<String, Object> selectNovelContentBySectionId(int novelId, int sectionId, int sort) {
        Map<String, Object> resultMap = new HashMap<>();

        //章节信息
        List<NovelSectionResultDto> sectionList = novelSectionMapper.selectFrontCurrentBackSectionInfo(novelId, sort - 1, sort + 1);


        if (sectionList.size() == 2) {
            NovelSectionResultDto sectionDto = new NovelSectionResultDto();
            if (sort == 1) {
                sectionList.add(0, sectionDto);
            } else {
                sectionList.add(sectionDto);
            }
        }

        resultMap.put("sectionInfo", sectionList);

        //小说正文
        NovelContentResultDto content = novelContentMapper.selectNovelContentBySectionId(sectionId);

        if (StringUtil.isNotEmpty(content.getContent()))
            content.setContent(content.getContent().split("---")[0]);

        resultMap.put("content", content);
        return resultMap;
    }

    @Override
    public List<NovelTypeResultDto> getNovelTypeByChannel(int channel) {
        return novelTypeMapper.selectNovelTypeByChannel(channel);
    }

    @Override
    public NovelNameResultDto selectNovelInfoByNovelId(int novelId) {
        return novelNameMapper.selectNovelInfoByNovelId(novelId);
    }

    @Override
    public int selectFirstSectionIdByNovelId(int novelId) {
        return novelSectionMapper.selectFirstSectionIdByNovelId(novelId);
    }
}
